/*
 * Copyright &copy; 2011-2020 lnint Inc. All right reserved.
 * 
 * 修改信息：【与SVN提交信息一致】
 * A: 新增类 位苏 2015-03-20 08:42:32
 */
package com.lnint.jess.generator.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lnint.jess.common.persistence.Page;
import com.lnint.jess.common.utils.StringUtils;
import com.lnint.jess.common.web.BaseController;
import com.lnint.jess.core.sys.entity.User;
import com.lnint.jess.core.sys.utils.UserUtils;
import com.lnint.jess.generator.entity.GenScene;
import com.lnint.jess.generator.entity.GenScheme;
import com.lnint.jess.generator.service.GenSceneService;
import com.lnint.jess.generator.service.GenSchemeService;
 
/**
 * <P>代码生成方案管理Controller</P>
 *
 * @version 2015-03-20
 * @author 位苏  2015-03-20 08:42:32
 * @since JDK 1.6
 */
@Controller
@RequestMapping(value = "${adminPath}/generator/genScheme")
public class GenSchemeController extends BaseController {

	@Autowired
	private GenSchemeService genSchemeService;
	@Autowired
	private GenSceneService genSceneService;
	
	@ModelAttribute
	public GenScheme get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return genSchemeService.get(id);
		}else{
			return new GenScheme();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(GenScheme genScheme, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			genScheme.setCreateBy(user);
		}
        Page<GenScheme> page = genSchemeService.find(new Page<GenScheme>(request, response), genScheme); 
        model.addAttribute("page", page);
		return "generator/genSchemeList";
	}

	@RequestMapping(value = "form")
	public String form(GenScheme genScheme, Model model) {
		List<GenScene> sceneList = genSceneService.findAll();
		model.addAttribute("sceneList", sceneList);
		model.addAttribute("genScheme", genScheme);
		return "generator/genSchemeForm";
	}

	@RequestMapping(value = "save")
	public String save(GenScheme genScheme, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, genScheme)){
			return form(genScheme, model);
		}
		genSchemeService.save(genScheme);
		addMessage(redirectAttributes, "操作生成方案'" + genScheme.getName() + "'成功<br/>");
		return "redirect:" + adminPath + "/generator/genScheme/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		genSchemeService.delete(id);
		addMessage(redirectAttributes, "删除生成方案成功");
		return "redirect:" + adminPath + "/generator/genScheme/?repage";
	}
}
